package com.example.mapping.common.core;

import com.example.mapping.common.exception.BaseErrorCodeEnum;
import lombok.Getter;
import lombok.Setter;

/**
 * 响应结果
 * @author cheng
 * @param <T>
 */
@Getter
@Setter
public class ResultResponse<T> {

    private static final long serialVersionUID = 1L;

    /**
     * 请求成功
     */
    private static final boolean REQUEST_SUCCESS = true;

    /**
     * 请求失败
     */
    private static final boolean REQUEST_FAIL = false;

    /**
     * 默认错误码
     */
    private static final String DEFAULT_ERROR_CODE = "-1";

    /**
     * 请求是否成功
     */
    private Boolean success;

    /**
     * 业务数据
     */
    private T data;

    /**
     * 错误码
     */
    private String errorCode;

    /**
     * 错误提示语
     */
    private String errorMessage;


    public ResultResponse(Boolean success, T data, String errorCode, String errorMessage) {
        this.success = success;
        this.data = data;
        this.errorCode = errorCode;
        this.errorMessage = errorMessage;
    }

    /**
     * 成功，不用返回数据
     * @return 响应结果
     */
    public static <T> ResultResponse<T> buildSuccess() {
        return new ResultResponse<>(REQUEST_SUCCESS, null, null, null);
    }

    /**
     * 成功，返回数据
     *
     * @param data 响应数据
     * @return 响应结果
     */
    public static <T> ResultResponse<T> buildSuccess(T data) {
        return new ResultResponse<>(REQUEST_SUCCESS, data, null, null);
    }

    /**
     * 失败，固定状态码
     *
     * @param errorMsg 错误信息
     * @return 响应结果
     */
    public static <T> ResultResponse<T> buildError(String errorMsg) {
        return new ResultResponse<>(REQUEST_FAIL, null, DEFAULT_ERROR_CODE, errorMsg);
    }

    /**
     * 失败，自定义错误码和信息
     *
     * @param errorCode 错误码
     * @param errorMsg  错误提示
     * @return 响应结果
     */
    public static <T> ResultResponse<T> buildError(String errorCode, String errorMsg) {
        return new ResultResponse<>(REQUEST_FAIL, null, errorCode, errorMsg);
    }

    /**
     * 失败，枚举类定义错误码和信息
     *
     * @param baseErrorCodeEnum 基础错误码枚举
     * @return 响应结果
     */
    public static <T> ResultResponse<T> buildError(BaseErrorCodeEnum baseErrorCodeEnum) {
        return new ResultResponse<>(REQUEST_FAIL, null, baseErrorCodeEnum.getErrorCode(), baseErrorCodeEnum.getErrorMsg());
    }

}
